sshd 安全加固

三月 02, 2026 #ssh

安装fail2ban

sudo apt install fail2ban
sudo systemctl enable --now fail2ban
sudo fail2ban status sshd

sshd 配置

# /etc/ssh/sshd_config

AllowUsers peter.home peter@192.168.2.*
AllowUsers peter@localhost peter@127.0.0.*

# ========== 全局安全策略 ==========
Port 22
Protocol 2
# 默认仅允许密钥登录(公网策略)
PasswordAuthentication no
PubkeyAuthentication yes
# 禁用不安全的认证方式
ChallengeResponseAuthentication no
KbdInteractiveAuthentication no
# 禁用 root 登录
PermitRootLogin no

# ========== 内网访问:允许密码登录 ==========
# 方案 A:基于服务器本地网卡地址
Match LocalAddress 192.168.* 10.* 172.16.* 172.17.*
    PasswordAuthentication yes
# 方案 B:基于客户端源地址(二选一,不要同时使用)
Match Address 192.168.0.0/16,10.0.0.0/8,172.16.0.0/12
    PasswordAuthentication yes